home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: judgemi@ix.netcom.com (Michael Judge )
- Newsgroups: comp.lang.c++
- Subject: Re: Why Do I Use An Ampersand in Member Class Parameters?
- Date: 31 Jan 1996 04:06:34 GMT
- Organization: Netcom
- Message-ID: <4empsa$18b@ixnews5.ix.netcom.com>
- References: <4emnv2$n5o@alcor.usc.edu>
- NNTP-Posting-Host: ix-or11-13.ix.netcom.com
- X-NETCOM-Date: Tue Jan 30 8:06:34 PM PST 1996
-
- In <4emnv2$n5o@alcor.usc.edu> wawda@alcor.usc.edu (Abu Wawda) writes:
- >
- >class Simple
- >{
- >public:
- > Simple();
- > int operator += (const Simple &);
- >private:
- > int data;
- >};
- >
- >I have seen many examples of this but I what I don't understand is why
- >there is an amersand after Simple? I would wind up implementating the
- >function as something like the following:
- >
- >int Simple::operator += (const Simple ¶meter)
- >{
- > data += parameter.data;
- >}
- >
- >I mean, I would understand if I parameter were a pointer to a Simple
- >class, but that is not the case, since I am not doing:
- >
- >int Simple::operator += (const Simple ¶meter)
- >{
- > data += parameter->data;
- >}
- >
- >Then why is there an amersand? I would appreciate any
- >suggestions. Thank you!
- >
- >
- >-Abu Wawda
- > wawda@scf.usc.edu
- >
- >
-
- I am not sure i understand this, but the & makes the parameter a
- reference. That way a copy of simple is not made when the method is
- used. Use & insted of * because you are more assured that the parameter
- is a legal object. (unless someone did some evil casting)
-
- note: i dont have my compiler with me but i dont think one simple
- object can necessarily access another's private members without an
- access method like int Simple::getData()const; If i am mistaken someone
- please correct me.
-